package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.common.UploadFileConstant;
import com.fdb.efp.nls.common.bo.UpLoadFileVO;
import com.fdb.efp.nls.service.facade.UpLoadBaiduFileService;
import com.fdb.efp.nls.service.facade.UpLoadFileService;
import com.sunyard.client2.FileNameEnums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service("upLoadBaiduFileService")
public class UpLoadBaiduFileServiceImp implements UpLoadBaiduFileService, FrameworkService {

	private final static Logger logger = LoggerFactory.getLogger(UpLoadBaiduFileServiceImp.class);
	@Autowired
	@Qualifier("upLoadFileService")
	private UpLoadFileService upLoadFileService;

	/**
	 * 
	 * 百度代偿文件上传影像系统
	 * 
	 * @param applySeq
	 * @param filePath
	 * @return
	 * @throws Exception
	 * @author liyakui
	 * @since 2019年1月26日 上午10:24:36
	 * @version 1.0
	 */
	@Override
	public boolean uploadBaiduFile(String applySeq, String urlFile) throws Exception {
		boolean result = false;
		try {
			String startDate = new SimpleDateFormat("yyyyMMdd").format(new Date());// 流水号生成时间 yyyymmdd
			UpLoadFileVO upLoadFileVO = new UpLoadFileVO();
			upLoadFileVO.setApplySeq(applySeq);
			upLoadFileVO.setStartDate(startDate);
			//证件号码 TODO
			//客户名称 TODO
			//产品名称 TODO
			String ret = UploadFileConstant.uploadFile(upLoadFileVO, urlFile);
			String[] rets = ret.split("<<::>>");
			File path = new File(urlFile);
			File[] files = path.listFiles();
			for (int i = 0; i < files.length; i++) {// 循环文件插入表
				UpLoadFileVO upLoadFileVo = new UpLoadFileVO();
				upLoadFileVo.setApplySeq(applySeq);
				String dateFile = files[i].getName().substring(files[i].getName().indexOf("_") + 1, files[i].getName().lastIndexOf("."));// 文件属性值
				if ("".equals(dateFile) || dateFile == null) {
					dateFile = "unkonw";
				}
				upLoadFileVo.setImageType(FileNameEnums.map.get(dateFile));
				upLoadFileVo.setFilePath(urlFile);
				upLoadFileVo.setStartDate(startDate);
				upLoadFileVo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				upLoadFileVo.setLastModifyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				// SUCCESS<<::>>20190116-E3BD0CCF-FE36-951F-88F3-38821BF7A50C-1
				// FAIL<<::>>20190116-F453F865-2103-7FC1-A605-1C39AD4799AE-1<<::>>743
				if (rets.length > 2) {// 失败
					upLoadFileVo.setState(rets[0]);// 状态
					upLoadFileVo.setBatch(rets[1]);// 返回批次号
					upLoadFileVo.setStateNum(rets[2]);// 失败状态码
				} else {
					upLoadFileVo.setState(rets[0]);// 状态
					upLoadFileVo.setBatch(rets[1]);// 返回批次号
				}
				upLoadFileService.insertUpLoadFile(upLoadFileVo);
			}

		} catch (Exception e) {
			logger.debug("上传百度代偿文件异常！" + e.getMessage());
			throw e;
		}
		return result;
	}

	@Override
	public boolean downLoadBaiduFile(String applySeq, String filePath) throws Exception {
		return false;
	}

}
